import DBConnection from "../../service/unit/mysql/DBConnection";
import FileStrategy from "../../service/file/File";

interface IData {
    id:number
    content:string
    title:string
}

export default async function updateArticleContent(conn:DBConnection, data:IData){
    const {id, content, title} = data;
    const path = await updateArticleContentQuery(conn, id);
    const File = FileStrategy;
    return File.writeFile(path, title, content);
}

async function updateArticleContentQuery(conn:DBConnection, id:number):Promise<string>{
    const _knex = conn.knex;
    const TABLE_NAME = 'article';
    const queryResult = await _knex.where({id:id}).select('path').table(TABLE_NAME);
    const {path} = queryResult[0];
    return conn.strDecode(path);

}